/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.lesson.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.biz.modular.lesson.param.*;
import vip.xiaonuo.biz.modular.timetable.service.TimetableService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.biz.modular.lesson.entity.EduLesson;
import vip.xiaonuo.biz.modular.lesson.service.EduLessonService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 排课控制器
 *
 * @author liye
 * @date  2023/08/06 10:18
 */
@Api(tags = "排课控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class EduLessonController {

    @Resource
    private EduLessonService eduLessonService;

    @Resource
    private TimetableService timetableService;

    /**
     * 获取排课分页
     *
     * @author liye
     * @date  2023/08/06 10:18
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取排课分页")
    @SaCheckPermission("/biz/lesson/page")
    @GetMapping("/biz/lesson/page")
    public CommonResult<Page<EduLesson>> page(EduLessonPageParam eduLessonPageParam) {
        return CommonResult.data(eduLessonService.page(eduLessonPageParam));
    }

    /**
     * 添加排课
     *
     * @author liye
     * @date  2023/08/06 10:18
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加排课")
    @CommonLog("添加排课")
    @SaCheckPermission("/biz/lesson/add")
    @PostMapping("/biz/lesson/add")
    public CommonResult<String> add(@RequestBody @Valid EduLessonAddParam eduLessonAddParam) {
        eduLessonService.add(eduLessonAddParam);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("创建排课")
    @CommonLog("创建排课")
    @SaCheckPermission("/biz/lesson/create")
    @PostMapping("/biz/lesson/create")
    public CommonResult<String> create(@RequestBody @Valid EduLessonCreateParam eduLessonCreateParam) {
        List<String> lectureIds = StrUtil.split(eduLessonCreateParam.getLectureIds(), StrUtil.COMMA);
        for (String lectureId : lectureIds) {
            EduLessonAddParam eduLessonAddParam = new EduLessonAddParam();
            eduLessonAddParam.setLectureId(lectureId);
            // 批量排课默认按短课时（2节连上）安排
            eduLessonAddParam.setExtended(false);
            eduLessonService.add(eduLessonAddParam);
        }
        return CommonResult.ok();
    }

    /**
     * 编辑排课
     *
     * @author liye
     * @date  2023/08/06 10:18
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑排课")
    @CommonLog("编辑排课")
    @SaCheckPermission("/biz/lesson/edit")
    @PostMapping("/biz/lesson/edit")
    public CommonResult<String> edit(@RequestBody @Valid EduLessonEditParam eduLessonEditParam) {
        eduLessonService.edit(eduLessonEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除排课
     *
     * @author liye
     * @date  2023/08/06 10:18
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除排课")
    @CommonLog("删除排课")
    @SaCheckPermission("/biz/lesson/delete")
    @PostMapping("/biz/lesson/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<EduLessonIdParam> eduLessonIdParamList) {
        eduLessonService.delete(eduLessonIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取排课详情
     *
     * @author liye
     * @date  2023/08/06 10:18
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取排课详情")
    @SaCheckPermission("/biz/lesson/detail")
    @GetMapping("/biz/lesson/detail")
    public CommonResult<EduLesson> detail(@Valid EduLessonIdParam eduLessonIdParam) {
        return CommonResult.data(eduLessonService.detail(eduLessonIdParam));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("检测排课冲突")
    @GetMapping("/biz/lesson/detect")
    public CommonResult<String> detect(EduLessonDetectParam eduLessonDetectParam) {
        int numberOfClash = timetableService.detect(eduLessonDetectParam.getLessonIds());
        return CommonResult.data(String.valueOf(numberOfClash));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("锁定排课")
    @CommonLog("锁定排课")
    @SaCheckPermission("/biz/lesson/lock")
    @PostMapping("/biz/lesson/lock")
    public CommonResult<String> lock(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       CommonValidList<EduLessonIdParam> eduLessonIdParamList) {
        eduLessonService.lock(eduLessonIdParamList);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("按模版批量更新排课")
    @CommonLog("按模版批量更新排课")
    @SaCheckPermission("/biz/lesson/update")
    @PostMapping("/biz/lesson/update")
    public CommonResult<String> update(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                     CommonValidList<EduLessonIdParam> eduLessonIdParamList) {
        eduLessonService.update(eduLessonIdParamList);
        return CommonResult.ok();
    }
}
